package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.Model.AskForLeave;
import com.hlw.hr_man.service.AskForLeaveService;
import com.hlw.hr_man.serviceImpl.AskForLeaveServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: GetApplyAflController
 * @Description: 处理获取所有的请假申请操作
 * @author 唐明
 * @date 2016年9月20日 上午10:08:49
 *
 */
public class GetApplyAflController extends HttpServlet{

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */ 
	private static final long serialVersionUID = 1L;
	
	AskForLeaveService aflService = new AskForLeaveServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		//获得当前页码
		int page = Integer.parseInt(req.getParameter("page"));
		//获得每页行数
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		//计算开始页码
		int start = (page-1)*pageSize;
		//获得请假申请列表
		List<AskForLeave> afls = aflService.getApplyAfl(start, pageSize);
		//获得请假申请总数
		int afl_count = aflService.getApplyAflCount();
		//将请假申请记录以json格式存放
		JSONObject currentEmpAbsents = new JSONObject();
		currentEmpAbsents.put("total", afl_count);
		currentEmpAbsents.put("rows", JSONArray.fromObject(afls));
		//将数据传回页面
		PrintWriter pw = resp.getWriter();
		pw.write(currentEmpAbsents.toString());
	}
}
